import resource from '../../../lib/resource';


//配置文件
var config = require("../../../config/basicsConfig.js");

//登录操作
var util = require("../../../utils/login.js");

const app = getApp();
Page({
  data: {
    loading: true,
    list: []
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getuserinfo();
  },
  onLoad() {
    console.log(app.globalData);
    this.getuserinfo();
  },
  navigateTo(e) {
    const url = e.currentTarget.dataset.url;
    const that = this;
    if (url === undefined) {
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success(res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths;
          console.log(tempFilePaths[0]);
          wx.uploadFile({
            url: resource.getUrl('/wx/upload'), // 仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              user: 'test'
            },
            success(res) {
              if (res.statusCode != 200) {
                resource.showTips(that, '图片上传失败');
                console.log(res);
                return;
              }
              var icon = res.data;
              resource.updateUserInfo({
                icon: icon
              }).then(res => {
                if (res.statusCode == 200) {
                  app.globalData.userInfo.avatarUrl = res.data.data.icon;
                  resource.showTips(that, '修改成功');
                  that.onLoad();
                } else {
                  resource.showTips(that, '修改失败');
                }
              });
            },
            fail(res) {
              console.log(res);
              resource.showTips(that, '图片上传失败');
            }
          });
        }
      });
    } else {
      wx.navigateTo({
        url
      });
    }
  },



  /**
   * 获取用户信息
   */
  getuserinfo: function () {
    wx.request({
      url: config.basics.website + config.requesturls.getuserinfo,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        'token': wx.getStorageSync('token'),
      },
      success: (res) => {
        //验证token
        let verificationToken = util.log.verificationToken(res.data.errCode);
        if (verificationToken == false) {
          wx.navigateBack({
            url: '../user/user?start=0'
          })
        }
        console.log("成功", res);
        var app = getApp();
        app.globalData.userInfo = res.data.datas;
        this.setData({
          list: res.data.datas,
        });

        console.log(this.data);
      },
      fail: (res) => {
        console.log("失败", res);
      }
    })
  },

  /**
   * 修改手机号
   */
  updatePhone: function () {
    wx.navigateTo({
      url: '../../phone/phone',
    })},


    /**
   * 授权手机号
   */
  getPhoneNumber(e) {
    console.log(123456,e);
    if(e.detail.errMsg=='getPhoneNumber:ok'){
      wx.request({
        url: config.basics.website + config.requesturls.updatephonenumberofuser,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          'token': wx.getStorageSync('token'),
          'errMsg': e.detail.errMsg,
          'iv': e.detail.iv,
          'encryptedData': e.detail.encryptedData,
          'state':1
        },
        success: (res) => {
            //设置成功
        wx.hideLoading()
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000 //持续的时间
        })
        this.onLoad();
        }
  
      })
    }
  }

});